package List;

public class MyLinkedList {

    public ListNode head;

    public void createList() {
        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(22);
        ListNode listNode3 = new ListNode(32);
        ListNode listNode4 = new ListNode(42);
        ListNode listNode5 = new ListNode(52);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        this.head = listNode1;
    }


    //打印链表
    public void print() {
        //createList();
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //头插法
    public ListNode addFirst(int data) {
        ListNode listNode = new ListNode(data);
        if (isEmpty()) {
            this.head = listNode;
            return null;
        }

        listNode.next = this.head;
        this.head = listNode;

        return this.head;
    }

    /**
     * 找到index-1位置的节点的地址
     * @param index
     * @return
     */
    public ListNode findIndex(int index) {
        ListNode cur = this.head;
        while (--index > 0) {
            cur =  cur.next;
        }
        return cur;
    }

    //任意位置插入, 第一个数据节点为0号的下标
    public void addIndex(int index, int data) {
        ListNode listNode = new ListNode(data);
        if (index < 0 || index > size()) {
            System.out.println("位置不合法");
            return;
        }
        if (index == 0) {
         addFirst(data);
         return;
        } else if (index == size()) {
         addLast(data);
         return;
        }
        ListNode cur = findIndex(index);
        listNode.next = cur.next;
        cur.next = listNode;
    }

    //尾插法
    public void addLast(int data) {
        ListNode listNode = new ListNode(data);
        if (isEmpty()) {
            this.head = listNode;
            return;
        }
        ListNode cur = this.head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = listNode;
    }

    //查找被删除节点的前驱
    public ListNode searchperv(int key) {
        ListNode cur = this.head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }


    //删除第一次出现关键词为key的节点
    public void remove(int key) {
        if (isEmpty()) {
            System.out.println("该列表为空  删除失败");
            return;
        }
        if (head.val == key) {
            this.head = this.head.next;
            return;
        }

        ListNode cur = searchperv(key);
        if (cur == null) {
            System.out.println("没有你要删除的节点");
        }
        ListNode del = cur.next;
        cur.next = del.next;

       /*
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.next.val == key) {
                cur.next = cur.next.next;
                return;
            }
            cur = cur.next;
        }*/


    }



    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = this.head;
        while(cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除所有值为key的节点
    public void removeAllkey(int key) {
        ListNode cur = this.head;
        ListNode del = this.head.next;

        /* while(del.next != null) {
            if (del.val == key) {
                del = del.next;
                continue;
            }else {
                cur.next = del;
                cur = cur.next;
                del = del.next;
            }
        }
       if (cur.next.val == key) {
            cur.next = null;
        }
        if (this.head.val == key) {
            this.head = this.head.next;
        }*/
       while (del != null) {
           if (del.val == key) {
               cur.next = del.next;
               del = del.next;
           } else {
               cur = del;
               del = del.next;
           }
       }
       //最后处理头
        if (this.head.val == key) {
            this.head = this.head.next;
        }

    }



    //得到链表长度
    public int size() {
        int count = 0;
        ListNode cur = this.head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }


    public void clear() {
        //this.head = null
        ListNode cur = this.head;
        ListNode curNext = this.head.next;
        while(curNext != null) {
            cur.next = null;
            cur = curNext;
            curNext = curNext.next;
        }
    }

    public boolean isEmpty() {
        if (this.head == null) {
            return true;
        }
        return false;
    }
}
